// pages/test/index.js
const {
  reviewList,
  reviewReceive
} = require('../../../api/api')
const {
  checkLogin
} = require('../../../utils/util')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navHeight: getApp().globalData.navHeight,
    navTop: getApp().globalData.navTop,
    menuHeight: getApp().globalData.menuHeight,
    height: 0,
    list: [],
    tabbarHeight: getApp().globalData.tabbarHeight,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    if (options !== undefined) {
      if (wx.getStorageSync('qrcode')) {
        const qrcode = JSON.parse(wx.getStorageSync('qrcode')) || []
        if (qrcode.length > 0) {
          checkLogin().then((res) => {
            this.reviewReceive()
          })
        }
      }
    }
    wx.hideTabBar()
    getApp().editTabbar()
    this.getList()
  },
  async reviewReceive() {
    const qrcode = JSON.parse(wx.getStorageSync('qrcode'))
    const params = {
      cardBatchId: qrcode[2],
      cardNumber: qrcode[0],
      cardPassword: qrcode[1],
    }
    wx.removeStorageSync('qrcode')
    const {
      result
    } = await reviewReceive(params)
    getApp().fetchUserInfo()
  },
  async getList() {
    const {
      result
    } = await reviewList()
    result.map((item) => {
      let value = JSON.parse(item.description)
      for (let key in value) {
        item[key] = value[key]
      }
    })
    this.setData({
      list: result
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    checkLogin().then(async (res) => {
      getApp().getMessageNum().then(allCount => {
        this.setData({
          allCount
        })
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */

  onLoadFunc() {},
  goto(e) {
    // checkLogin().then((res) => {
      const index = e.currentTarget.dataset.index
      const param = this.data.list[index]
      if (param.yid) {
        let title = (param.shortTitle != ' ' ? param.shortTitle : '') + param.title
        this.onNavigate(param.yid, title)
      } else {
        wx.navigateTo({
          url: '/pages/test/desc/index?param=' + encodeURIComponent(JSON.stringify(param)),
        })
      }
    // }).catch((e) => {
    //   wx.showToast({
    //     title: '请先登录！',
    //     icon: 'none'
    //   })
    //   setTimeout(() => {
    //     wx.navigateTo({
    //       url: '/pages/login/index',
    //     })
    //   }, 500);
    //
    // })
  },
  onNavigate(id, title) {
    let phone = wx.getStorageSync('userInfo').mobile
    let callbackUrl = encodeURIComponent(`pages/users/resultY/index?yid=${id}&title=${title}`)
    wx.navigateToMiniProgram({
      appId: 'wx52fda56d0b94a6a9',
      path: `subpages/evaluation/detail?id=${id}&phoneNumber=${phone}&provider=ddsx&callbackUrl=${callbackUrl}`,
      extraData: {
        // 额外的数据
      },
      success(res) {
        // 跳转成功的回调函数
      },
      fail(res) {
        // 跳转失败的回调函数
      },
      complete(res) {
        // 不论跳转成功或失败，都会调用的回调函数
      }
    })
  },
  my() {
    checkLogin().then((res) => {
      wx.navigateTo({
        url: '/pages/users/test/index'
      })
    }).catch((e) => {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      }, 500);
    })
  },
  onShareAppMessage: function () {
    return {
      title: '职业测评',
      imageUrl: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  },
})
